package com.fate.domain;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.checkerframework.checker.units.qual.A;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 系统用户
 * 专门做一个登录的实体类
 */
@ApiModel(value="com-fate-domain-SysUser")
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "sys_user")
public class SysLoginUser implements UserDetails {
    @TableId(value = "user_id", type = IdType.INPUT)
    @ApiModelProperty(value="")
    private Long userId;

    /**
     * 用户名
     */
    @TableField(value = "username")
    @ApiModelProperty(value="用户名")
    private String username;

    /**
     * 密码
     */
    @TableField(value = "`password`")
    @ApiModelProperty(value="密码")
    private String password;



    /**
     * 状态  0：禁用   1：正常
     */
    @TableField(value = "`status`")
    @ApiModelProperty(value="状态  0：禁用   1：正常")
    private Byte status;



    /**
     * 用户所在的商城Id
     */
    @TableField(value = "shop_id")
    @ApiModelProperty(value="用户所在的商城Id")
    private Long shopId;

    @TableField(exist=false)
    @ApiModelProperty(value = "后台管理用户的权限")
    private List<String> auths;

    /**
     * 权限
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<SimpleGrantedAuthority> authorities = new ArrayList<>();
        if (!CollectionUtils.isEmpty(auths)){
            auths.forEach(auth ->{
                    //null,分割
                    if(StringUtils.hasText(auth)) {
                        //分割
                        String[] split = auth.split(",");
                        for (String s:split){
                            authorities.add(new SimpleGrantedAuthority(s));
                        }
                    }
            });
            }
        return authorities;
    }


    /**
     * 因为用id特别多
     * SecurityContextHolder.getContext().getAuthentication().getPrincipal() 用户名
     * 用户名
     * @return
     */
    public String getUsername(){

        return String.valueOf(this.userId)  ;
    }

    /**
     * 密码
     * @return
     */
    public String getPassword(){
        return  this.password;
    }



    @Override
    public boolean isAccountNonExpired() {
        return this.status ==1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status ==1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status ==1;
    }

    @Override
    public boolean isEnabled() {
        return this.status ==1;
    }
}